
package com.javaleague.sesionUsuario;

import com.javaleague.persistencia.DAOAvatar;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author TaixMiguel
 */
public class UpdateAvatar extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        int error = 403;
        if (request.getParameter("avatar") != null) {
            HttpSession session = request.getSession(true);
            Usuario user = (Usuario)session.getAttribute("usuario");
            if (user != null) {
                String url = request.getParameter("avatar");

                if (DAOAvatar.actualizarAvatar(user.getId(), user.getTokenUser(), url, user.getNombre(), request.getRemoteAddr())) {
                    user.setAvatar(url);
                    session.setAttribute( "usuario", user);
                    error = 200;
                } else
                    error = 604;
            }
        }        
        response.sendError(error);
    }
    
    
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}